package com.xgj.ph.dao.impl;

import com.xgj.ph.dao.UserDao;
import com.xgj.ph.entity.User;
import com.xgj.ph.util.JDBCDruidUtil;

import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDaoImpl implements UserDao {
    @Override
    public User selectByUsernameAndPwd(String username, String pwd) {
        String sql = "select * from user where username=? and pwd=?";
        Object[] obj = {username, pwd};
        User user = null;
        ResultSet res = JDBCDruidUtil.query(sql, obj);
        try {
            if(res.next()){
                user = new User(res.getInt(1), res.getString(2), res.getString(3));
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        JDBCDruidUtil.close();
        return user;
    }
    //  注册账户
    @Override
    public int insertUser(String username ,String pwd) {
        String sql = "insert into user values(?,?,?)";
        Object[] obj = {null,username, pwd};
        int num = 0;
        num = JDBCDruidUtil.update(sql, obj);
        JDBCDruidUtil.close();
        return num;
    }
}
